﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;

namespace AppFuscator
{
    class VisualProject : Project
    {
        const string FileFilter = "Appfuscator project (*.afproj)|*.afproj|All files (*.*)|*.*";

        [JsonIgnore]
        private ControlListView list;

        public VisualProject(ControlListView ctrlListView)
        {
            this.list = ctrlListView;
        }

        public override void Add(AssemblyItem item)
        {
            base.Add(item);

            TileListItem ctrlItem = new TileListItem();
            list.Add(ctrlItem);

            if (MainAssemble == null)
            {
                MainAssemble = item;
                list.SelectedItem = ctrlItem;
            }

            RefreshItem(item);
        }

        public void RefreshItem(AssemblyItem item)
        {
            int id = Assemblies.IndexOf(item);
            if (id == -1) return;

            var ctrl = list[id];
            ctrl.Name = item.Title;
            ctrl.Version = "1.0.0.0";

            if (item == MainAssemble)
                ctrl.Image = Properties.Resources.exe;
            else if (item.MergeAssemble == true)
                ctrl.Image = Properties.Resources.Linked;
            else
                ctrl.Image = Properties.Resources.dll;
        }

        public override void Delete(AssemblyItem asm)
        {
            if (asm != null)
            {
                int id = Assemblies.IndexOf(asm);
                if (id != -1)
                    Delete(id);
            }
        }

        public override void Delete(int id)
        {
            list.RemoveAt(id);
            base.Delete(id);
        }

        [JsonIgnore]
        public AssemblyItem SelectedItem
        {
            get
            {
                return GetItem(list.SelectedItem);
            }
        }

        public AssemblyItem GetItem(TileListItem item)
        {
            var id = list.IndexOf(item);
            if (id == -1) return null;
            return Assemblies[id];
        }

        public override AssemblyItem MainAssemble
        {
            get
            {
                return base.MainAssemble;
            }
            set
            {
                var old = base.MainAssemble;
                base.MainAssemble = value;
                if (base.MainAssemble != null) RefreshItem(base.MainAssemble);
                if (old != null) RefreshItem(old);
            }
        }

        public void Save()
        {
            if (!string.IsNullOrEmpty(ProjectFileName))
            {
                Save(ProjectFileName);
                return;
            }

            System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();
            dialog.Filter = FileFilter;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Save(dialog.FileName);
            }
        }

        public static VisualProject Load(ControlListView list)
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = FileFilter;
            dialog.Multiselect = false;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return Load(dialog.FileName, list);
            }

            return null;
        }

        public static VisualProject 
            Load(string fileName, ControlListView list)
        {
            var rt = Load<VisualProject>(fileName);
            if (rt == null) return null;
            list.Clear();
            rt.list = list;
            foreach (var it in rt.Assemblies)
            {
                rt.list.Add(new TileListItem());
                rt.RefreshItem(it);
            }
            return rt;
        }
    }
}
